﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace InputDocument.Models
{
    /// <summary>
    /// 读取txt文件中的数据并输出平均值、最大最小成绩者的信息
    /// </summary>
    class StudentServiceForTxt
    {
        /// <summary>
        /// 读取txt中的数据并保存到studentsA
        /// </summary>
        /// <returns></returns>
        public static List<Student> ReadFromTxt()
        {
            var students = new List<Student>();
            string filePath = @"学生.txt";//确定文件的相对路径
            StreamReader streamReader = new StreamReader(filePath, Encoding.Default);//此行代码？？？
            string strLine = streamReader.ReadLine();//读取信息行
            while (strLine != null)
            {
                students.Add(new Student(strLine));//此行代码？？？
                strLine = streamReader.ReadLine();//读取下一行？
            }
            return students;
        }
        /// <summary>
        /// 输出数据中的学生成绩的平均值，最高成绩及最低成绩的学生信息
        /// </summary>
        /// <param name="students"></param>
        public static void OutputScore(List<Student> students)
        {
            if (!students.Any())//判断数据是否为空,？
            {
                Console.WriteLine("there is no record!");
                return;
            }
            var average = students.Average(a => a.Score);//求成绩的平均值？
            Console.WriteLine($"txt中成绩的平均值为{average}");
            var max = students.Max(a => a.Score);
            var min = students.Min(a => a.Score);
            foreach (var item in students)//求出并输出最大最小成绩学生信息
            {
                if (item.Score == max)
                    Console.WriteLine($"成绩最高分:{max}分,对应学生姓名:{item.Name},学号:{item.StudentNumber},电话:{item.PhoneNumber},生日:{item.Birthday.ToLongDateString()},年龄:{item.Age}");//这里的birthday转换了？
                if (item.Score == min)
                    Console.WriteLine($"成绩最低分:{min}分,对应学生姓名:{item.Name},学号:{item.StudentNumber},电话:{item.PhoneNumber},生日:{item.Birthday.ToLongDateString()},年龄:{item.Age}");//这里的birthday转换了？
            }
        }
    }
}
